/*
 * ModelPool.cpp
 *
 *  Created on: 21.10.2012
 *      Author: Martin
 */

#ifdef _WIN32 
#include <windows.h> 
#endif

#include "ModelPool.h"

ModelPool::ModelPool()
{
}

ModelPool::~ModelPool()
{
	files.clear();
}

int ModelPool::getModelByName(std::string filename)
{
	for (unsigned int i = 0; i < files.size(); i++)
	{
		if (files[i]->getFName().compare(filename) == 0)
			return i;
	}

	std::cerr << "\nMODEL POOL: Model " << filename << " not found." << std::endl;

	return -1; // error
}

void ModelPool::drawModel(std::string filename)
{
	int ID;

	ID = getModelByName(filename);

	files[ID]->draw();
}

void ModelPool::drawModelByID(GLuint id)
{
	files[id]->draw();

	//glCallList(id);
}

void ModelPool::drawBBoxByModelID(GLuint id, glm::vec3 pos, float angle)
{
	files[id]->drawBoundingBox(pos, angle);
}

std::vector<Model*> ModelPool::getModels()
{
	return files;
}

int ModelPool::getCount()
{
	return files.size();
}

int ModelPool::loadModel(std::string filename, std::string exfile)
{
	// check if model is in memory

	for (unsigned int i = 0; i < files.size(); i++)
	{
		if (files[i]->getFName().compare(filename + exfile) == 0)
			return i;
	}

	Model *mbuff = new Model(filename, Engine::fileStructure.modelsPath);

#ifdef _DEBUG
	std::cout << "\nMODEL POOL: Loading model " << filename << std::endl;
	gCon::con->print("MODEL POOL: Loading model %s", filename.c_str());
#endif

	// loading model using Model.h

	// NEW !!!
	mbuff->loadModel(exfile);
	// -----------------------------------------------------------

	files.push_back(mbuff);

	return files.size() - 1;
}

